<?php
/* 
* @Author: LDF QQ 47121862
* @Date:   2014-05-30 20:42:48
* @Last Modified by:   LDF QQ 47121862
* @Last Modified time: 2014-06-06 20:49:15
*/
//填写订单注册登录ajax
class UserControl extends Control{
	public function out(){
		// $_SESSION=null;
		session_unset();
		session_destroy();
		go(U('Index/index'));
	}
	//验证码
	function code(){
		$code=New code();
		$code->show();
	}
	//获取登陆模板
	public function getLogin(){
		$path = ROOT_PATH . 'Template/default/User/hlogin.html';
		$c = $this->fetch($path);
		echo $c;die;
	}
	//获取注册模板
	public function getReg(){
		$path = ROOT_PATH . 'Template/default/User/hreg.html';
		$c = $this->fetch($path);
		echo $c;die;
	}
	public function reg(){
		$username=Q('username');
		$pwd=Q('pwd');
		if(empty($username)){
			echo 0;exit;
		}
		if(empty($pwd)){
			echo 0;exit;
		}
		// 查找数据库是否已注册
		$re=M('user')->where(array('username'=>$username))->find();
		if($re){
			echo 0;exit;
		}else{// 注册用户
			$data=array(
				'username'=>$username,
				'password'=>md5($pwd)
			);
			$id=M('user')->add($data);
			//注册成功写入SESSION
			$_SESSION['uid']=array(
				'uid'=>$id,
				'uname'=>$username
			);
			echo $id;exit;
		}
	}
	public function login(){
		$username=Q('username');
		$pwd=Q('pwd');
		if(empty($username)){
			echo 0;exit;
		}
		if(empty($pwd)){
			echo 0;exit;
		}
		$re=M('user')->where(array('username'=>$username))->find();
		if($re){
			if($re['password']==md5($pwd)){
				//登陆成功写入SESSION
				$_SESSION['uid']=array(
					'uid'=>$re['uid'],
					'uname'=>$username
				);
				//查找收货地址，并写入SESSION
				$address=M('shop_address')->where(array('uid'=>$re['uid'],'def'=>1))->find();
				$_SESSION['address']=$address;
				echo 1;exit;
			}else{
				echo 0;exit;
			}
		}else{
			echo 0;exit;
		}
		
	}
	//用户中心
	function user(){
		// C('TPL_ERROR',TPL_PATH.'/define/error.html');
		// C('TPL_SUCCESS',TPL_PATH.'/define/success.html');
		// $this->success('出错了你 可节省大富科技');
		$uid=$_SESSION['uid']['uid'];
		if(!$uid) go(U('Index/index'));
		if(IS_POST){
			$oldPass = Q('oldPass');
			$newPass = Q('newPass');
			$user=M('user')->where('uid='.$uid)->find();
			if(md5($oldPass)==$user['password']){
				M('user')->where('uid='.$uid)->save(array('password'=>md5($newPass)));
				go(U('Index/index'));
			}else{
				$url=U('Index/index');
				echo "
				<script>
					alert('密码不正确');
					location.href='$url';
				</script>
				";
			}
			// p($_POST);
			// p($user['username']);
		}else{
			$order=M('order')->where('uid='.$uid)->all();
			foreach ($order as $k=>$v) {
				$SQL = "SELECT g.index_pic,g.gname,g.gid FROM hd_g_order_list AS ol JOIN hd_g_stock AS s ON ol.stid=s.stid JOIN hd_g_goods AS g ON s.gid=g.gid WHERE ol.oid = ".$v['oid'];
				$data = M()->query($SQL);
				$order[$k]['goods']=$data;
			}
			$order=orderStat($order);
			// p($order);
			$this->assign('order',$order);
			$this->display();
		}
	}
	//删除订单
	function delOrder(){
		$oid = Q('oid',0,'intval');
		// echo $oid;
		// 删除订单order和order_list数据
		M('order')->where(array('oid'=>$oid))->del();
		M('order_list')->where(array('oid'=>$oid))->del();
		go(U('User/user'));
	}
	//查看订单
	function seeOrder(){
		$oid = Q('oid',0,'intval');
		$order=M('order')->where('oid='.$oid)->find();
		// p($order);
		echo json_encode($order);die;
	}
}
